package com.shanghai.shanghaielectric.UI.DilaogView;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import shanghaielectric_lib.Utils.MySystemUtil;

/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class Dialog_Password extends DialogFragment {
    public int id = 0;
    public static boolean IS_INPUT_ADMIN_KEYS = false;
    public static final int ERROR_KEYS = 789;

    public interface PassWordsListener {
        void getBack(int id, boolean isPassWordsCorrect);
    }

    public static   PassWordsListener mPassWordsListener;

    public static   void setPasswordsListener(PassWordsListener passWordsListener) {
        mPassWordsListener = passWordsListener;
    }

    public Context mContext;
    public String inputKeys = "";
    @BindView(R.id.tv_forgetKeys)
    TextView tvForgetKeys;
    @BindView(R.id.bt_cancel)
    Button btCancel;
    @BindView(R.id.bt_certain)
    Button btCertain;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_keys)
    EditText etKeys;
    Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == ERROR_KEYS) {
                tvTitle.setText(String.valueOf(msg.obj));
                tvTitle.setTextColor(getResources().getColor(R.color.red_color));
            }
        }
    };
    public void setCheckId(int id) {
        this.id = id;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.m_dialog_pasword, container);
        ButterKnife.bind(this, view);
//        Toast.makeText(mContext, this.getTag().toString(), Toast.LENGTH_SHORT).show();
        initData();
        return view;
    }

    private void initData() {
        IS_INPUT_ADMIN_KEYS = false;//g管理员密码
        mContext = MyApplication.getContext();
    }

    @OnClick({R.id.bt_cancel, R.id.bt_certain, R.id.tv_forgetKeys})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_cancel:
                //隐藏软键盘
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                dismiss();
                break;
            case R.id.tv_forgetKeys:
                Message mes = new Message();
                mes.what = ERROR_KEYS;
                mes.obj = "请输入管理员密码：";
                hander.sendMessage(mes);
                //密码错误，返回false
                IS_INPUT_ADMIN_KEYS = true;//g管理员密码
                break;
            case R.id.bt_certain:
                if (TextUtils.isEmpty(etKeys.getText())) {
                    Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    //获得密码
                    inputKeys = etKeys.getText().toString();
                    if (IS_INPUT_ADMIN_KEYS) {
                        //输入管理员密码逻辑
                        if (inputKeys.equals(Constants.ADMINI_PASSWORDS)) {
//密码正确，返回true
                            mPassWordsListener.getBack(id,true);
                            dismiss();
                        } else {
                            Message mes1 = new Message();
                            mes1.what = ERROR_KEYS;
                            mes1.obj = "密码错误，请重新输入：";
                            hander.sendMessage(mes1);
                            //密码错误，返回false
                            mPassWordsListener.getBack(id,false);

                        }
                    } else {
                        //输入用户密码逻辑
                        if (MySystemUtil.checkPassWords(inputKeys)) {
                            //密码正确，返回true
                            mPassWordsListener.getBack(id,true);
                            dismiss();
                        } else {
                            Message mes1 = new Message();
                            mes1.what = ERROR_KEYS;
                            mes1.obj = "密码错误，请重新输入";
                            hander.sendMessage(mes1);
                            //密码错误，返回false
                            mPassWordsListener.getBack(id,false);

                        }
                    }

                }
                break;
        }
    }


}
